//
//  FriendGameCell.m
//  CandyCrushClone
//
//  Created by Petar on 2/26/15.
//  Copyright (c) 2015 Nestorov Engineering. All rights reserved.
//

#import "FriendGameCell.h"

@interface FriendGameCell ()

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UIButton *inviteButton;

@end

@implementation FriendGameCell
{
    NSIndexPath *_indexPath;
    BOOL _isAlreadyInvited;
}

- (void)awakeFromNib {
    // Initialization code
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


- (void)configureWithName:(NSString *)username isOnline:(BOOL)isOnline forIndexPath:(NSIndexPath *)indexPath
{
    
    _nameLabel.text = username ? username : @"";
    _indexPath = indexPath;
    
    if ( isOnline == NO ) {
        
        [_inviteButton setTitle:@"offline" forState:UIControlStateNormal];
        [_inviteButton setEnabled:NO];
        
    } else {
        
        [_inviteButton setTitle:@"Invite" forState:UIControlStateNormal];
        [_inviteButton setEnabled:YES];
    }
    
    if ( _isAlreadyInvited == YES ) {
        
        [_inviteButton setTitle:@"Invited" forState:UIControlStateNormal];
        [_inviteButton setEnabled:NO];

    }
    
}


- (IBAction)didPressInviteButton:(id)sender
{
    
    [_inviteButton setTitle:@"Invited" forState:UIControlStateNormal];
    [_inviteButton setEnabled:NO];
    
    _isAlreadyInvited = YES;
    
    if ( self.delegate && [self.delegate respondsToSelector:@selector(didPressInviteButtonAtIndexPath:)] ) {
        [self.delegate didPressInviteButtonAtIndexPath:_indexPath];
    }
    
}

@end
